/*
 * @Author: blak-kong
 * @Date: 2019-12-04 14:46:05
 * @Email: 546598185@qq.com
 */
const router = require('koa-router')()
router.prefix('/api')

const multer = require('koa-multer')
//文件上传
//配置
var storage = multer.diskStorage({
    //文件保存路径
    destination: function (req, file, cb) {
      cb(null, 'public/uploads/')
    },
    //修改文件名称
    filename: function (req, file, cb) {
      var fileFormat = (file.originalname).split(".");  //以点分割成数组，数组的最后一项就是后缀名
      cb(null,Date.now() + "." + fileFormat[fileFormat.length - 1]);
    }
})
//加载配置
var upload = multer({ storage: storage });
//路由
router.post('/upload',upload.single('file'),async(ctx,next)=>{
    ctx.body = {
        filename: ctx.req.file.filename//返回文件名
    }
})

module.exports = router;